 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : KindlePush
 * Package  : net.solosky.kindlepush.forum
 * File     : TianyaForumProvider.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2012-4-15
 * License  : Apache License 2.0 
 */
package net.solosky.kindlepush.forum.provider;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.solosky.kindlepush.forum.bean.Post;
import net.solosky.kindlepush.forum.bean.Topic;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

/**
 *
 * 天涯论坛接口的实现
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class TianyaForumProvider implements ForumProvider{
	
	@Override
	public Topic parseTopic(String url) throws IOException {
		Topic topic = new Topic();
		Document doc = Jsoup.connect(url).timeout(30*1000).get();
		String title = doc.select("#hTitle>span").text();
		String first = doc.select("#firstAuthor").text();	//作者：hyqiankai  发表日期：2012-1-22 18:22:00
		Pattern ptFirst = Pattern.compile("^作者：(.+?)  发表日期：(.+)$");
		Matcher mcFirst = ptFirst.matcher(first);
		if(mcFirst.matches()){
			String author = mcFirst.group(1);
			String postDate = mcFirst.group(2);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
			topic.setAuthor(author);
			try {
				topic.setPostDate(sdf.parse(postDate));
			} catch (ParseException e) {
				topic.setPostDate(new Date());
			}
		}
		String pageIdVal = doc.select("input[name=idArticleslist]").val();
		String [] pageIds = pageIdVal.split(",");
		String prefixUrl = url.substring(0, url.lastIndexOf("/"));
		List<String> pageUrls = new ArrayList<String>();
		for(String pageId: pageIds){
			pageUrls.add(prefixUrl+"/"+pageId+".shtml");
		}
		
		topic.setTitle(title);
		topic.setMainUrl(url);
		topic.setPageNum(pageIds.length);
		topic.setPageUrls(pageUrls);
		topic.setId(pageIds[0]+"");

		return topic;
	}

	@Override
	public List<Post> parsePosts(String url) throws IOException {
		List<Post> posts = new ArrayList<Post>();
		Document doc = Jsoup.connect(url).timeout(60*1000).get();
		Elements postElements  = doc.select("#pContentDiv .post");	//帖子内容信息
		Elements tableElements = doc.select("#pContentDiv table");	//含有作者信息
		tableElements.add(0, doc.select("#firstAuthor").first());	//每页第一个帖子作者比较特殊
		
		for(int i=0; i<tableElements.size(); i++){
			Post post = new Post();
			String postInfo = tableElements.get(i).text();
			Pattern ptInfo = Pattern.compile("^作者：(.+?)(?:　回复|  发表)日期：(.+)$");
			Matcher mcInfo = ptInfo.matcher(postInfo.trim());
			if(mcInfo.matches()){
				String author = mcInfo.group(1);
				String postDate = mcInfo.group(2);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
				post.setAuthor(author);
				try {
					post.setDate(sdf.parse(postDate));
				} catch (ParseException e) {
					post.setDate(new Date());
				}
			}
			Whitelist whitelist = Whitelist.basic();
            whitelist.addTags("img")
	            .addAttributes("img", "alt", "title", "original")
	            .addProtocols("img", "original", "http", "https");
			Cleaner cleaner = new Cleaner(whitelist);
			Document cleanDoc = cleaner.clean(Jsoup.parse(postElements.get(i).html()));
			
			Elements imgs = cleanDoc.select("img");
			for(int j=0; j<imgs.size(); j++){
				Element imgElm = imgs.get(j);
				String imgUrl = imgElm.attr("original");
				imgElm.removeAttr("original");
				imgElm.attr("src", imgUrl);
			}
			
			post.setText(cleanDoc.body().html());
			posts.add(post);
		}
		return posts;
	}
	
	public static void main(String[] args) throws IOException {
		ForumProvider provider = new TianyaForumProvider();
		Topic topic = provider.parseTopic("http://www.tianya.cn/publicforum/content/funinfo/1/3072613.shtml");
		for(String url: topic.getPageUrls()){
			System.out.println("FETCH POSTS:"+url);
			List<Post> posts = provider.parsePosts(url);
			for(Post post:posts){
				System.out.println(post.getDate()+"=>"+post.getAuthor());
				System.out.println(post.getText());
				System.out.println("-------------------------------------------------------------------------");
			}
		}
	
		//System.out.println(topic.getPageUrlList());
		//System.out.println(topic.getTitle());
	}

}
